import java.lang.Thread;
import java.lang.Runtime;
import java.io.*;

public class MixLauncher {

	public static void main(String[] args) {
		int nb = 0;
		try {
			nb = Integer.valueOf(args[0]);
		} catch (Exception exc) {
			System.err.println("CounterLauncher <nbProcess>");
			exc.printStackTrace();
			System.exit(0);
		}

		Process rmireg = null;
		Process server = null;
		Process[] counter = null;
		Process[] stresser = null;
		try {
			Runtime rt = Runtime.getRuntime();
			rmireg = rt.exec("rmiregistry");
			Thread.sleep(3000);
			server = rt.exec("java Server");
			Thread.sleep(1000);

			System.out.println("Initialization.");
			Client.init();
			Count_itf c = (Count_itf)Client.lookup("CPT");
			if(c == null) {
				c = (Count_itf)Client.create(new Count());
				Client.register("CPT", c);
			}
			Client.init();
			Sentence_itf c2 = (Sentence_itf)Client.lookup("Stresser");
			if(c2 == null) {
				c2 = (Sentence_itf)Client.create(new Sentence());
				Client.register("Stresseur", c2);
			}
			System.out.println("System initialized.");

			counter = new Process[nb];

			for(int i = 0 ; i < nb ; i++) {
				counter[i] = rt.exec("java Counter");
				if(i == 0 || i == nb - 1) {
					System.out.println("Counter " + (i+1) + " launched.");
				} else if(i == 1) {
					System.out.println("Counter...");
				}
			}

			stresser = new Process[nb];
			for(int i = 0 ; i < nb ; i++) {
				stresser[i] = rt.exec("java Stresser");
				if(i == 0 || i == nb - 1) {
					System.out.println("Stresser " + (i+1) + " launched.");
				} else if(i == 1) {
					System.out.println("Stresser...");
				}
			}

			for(int i = 0 ; i < nb ; i++) {
				System.out.println("Counter : " + (i+1));
				BufferedReader br = new BufferedReader(new InputStreamReader(counter[i].getInputStream()));
				String s = null;
				while((s = br.readLine()) != null) {
					System.out.println(s);
				}

				br = new BufferedReader(new InputStreamReader(counter[i].getErrorStream()));
				while((s = br.readLine()) != null) {
					System.err.println(s);
				}
			}

			for(int i = 0 ; i < nb ; i++) {
				System.out.println("Stresser : " + (i+1));
				BufferedReader br = new BufferedReader(new InputStreamReader(stresser[i].getInputStream()));
				String s = null;
				while((s = br.readLine()) != null) {
					System.out.println(s);
				}

				br = new BufferedReader(new InputStreamReader(stresser[i].getErrorStream()));
				while((s = br.readLine()) != null) {
					System.err.println(s);
				}
			}

			c = (Count_itf)Client.lookup("CPT");
			c.lock_read();
			System.out.println("Final value : " + c.read());
			c.unlock();

			for(int i = 0 ; i < nb ; i++) {
				System.out.print("Waiting for counter " + (i+1) + " to finish...");
				//counter[i].waitFor();
				counter[i].destroy();
				System.out.println("OK !");
			}

			for(int i = 0 ; i < nb ; i++) {
				System.out.print("Waiting for stresser " + (i+1) + " to finish...");
				//counter[i].waitFor();
				stresser[i].destroy();
				System.out.println("OK !");
			}

			/*
			System.out.println("Server");
			BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream()));
			String s = null;
			while((s = br.readLine()) != null) {
				System.out.println(s);
			}
			br = new BufferedReader(new InputStreamReader(server.getErrorStream()));
			s = null;
			while((s = br.readLine()) != null) {
				System.err.println(s);
			}
			//*/

		} catch(Exception exc) {
			exc.printStackTrace();
		} finally {
			rmireg.destroy();
			server.destroy();
			for(int i = 0 ; i < nb ; i++) {
				counter[i].destroy();
			}
			for(int i = 0 ; i < nb ; i++) {
				stresser[i].destroy();
			}

		}

		System.exit(0);
	}

}

